<?php

/**
 * This file is part of ossecdb-extjs.
 * 
 * Copyright (C) 2011 Brendan Johnston
 * 
 * Contact: brendan@johnston.net.au
 * 
 * Project: http://code.google.com/p/ossecdb-extjs/
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

$queries = (object) array();

//:~

$queries->location_key = 'id';

$queries->location_col = array(
	'id'		=> 'id',
	'server_id'	=> 'server_id',
	'host'		=> 'substring_index(name, \'->\', 1)',
	'path'		=> 'substring_index(name, \'->\', -1)'
);

$queries->location_from = 'location';

$queries->location = 
	'select ' . 
		ossec_views::cols_to_select($queries->location_col) . ' ' .
	'from ' . 
		$queries->location_from;

//:~

$queries->alert_key = 'alert_id';

// For use in ossec_filter::ip, has to perform like (eg '%.NUM.%.%') on IP addresses.
$queries->alert_map = array(
	'src_ip_str'	=> 'inet_ntoa(a.src_ip)',
	'dst_ip_str'	=> 'inet_ntoa(a.dst_ip)'
);

$queries->alert_col = array(
	'alert_id'	=> 'a.id',
	'time'		=> 'a.timestamp',
	'rule_id'	=> 'r.rule_id',
	'desc'		=> 'r.description',
	'level'		=> 'r.level',
	'loc_host'	=> 'substring_index(l.name, \'->\', 1)',
	'loc_path'	=> 'substring_index(l.name, \'->\', -1)',
	'src_ip'	=> 'a.src_ip',
	'src_port'	=> 'a.src_port',
	'dst_ip'	=> 'a.dst_ip',
	'dst_port'	=> 'a.dst_port',
	'user'		=> 'd.user',
	'full_log'	=> 'd.full_log'
);

$queries->alert_from = 
	'alert a ' . 
	'inner join signature as r on r.rule_id = a.rule_id ' . 
	'inner join server as s on a.server_id = s.id ' . 
	'inner join location as l on a.location_id = l.id ' . 
	'inner join data as d on a.id = d.id '
;

require('queries-mysql-geolocation.php');

$queries->alert = 
	'select ' . 
		ossec_views::cols_to_select($queries->alert_col) . ' ' .
	'from ' . 
		$queries->alert_from;
